@extends('layouts.application')

@section('content')

    <div class="admin-content">
        <div class="admin-content-body">
            <div class="am-cf am-padding am-padding-bottom-0">
                <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">商品管理</strong> /
                    <small>Brand</small>
                </div>
            </div>

            @include('layouts.shared._flash')

            <hr>
            <div class="am-g">
                <div class="am-u-sm-12 am-u-md-6">
                    <div class="am-btn-toolbar">
                        <div class="am-btn-group am-btn-group-xs">
                            <a href="{{Route('shop.brand.create')}}" class="am-btn am-btn-default">
                                <span class="am-icon-plus"></span> 新增
                            </a>
                            <button type="button"  class="delete_all am-btn am-btn-default">
                                <span class="am-icon-trash-o"></span> 删除
                            </button>
                        </div>
                    </div>
                </div>
                <div class="am-u-sm-12 am-u-md-3">
                    <div class="am-form-group">
                        <select data-am-selected="{btnWidth: '100%',  btnStyle: 'secondary', btnSize: 'sm', maxHeight: 260, searchBox: 1}">
                            <option value="option1">所有类别</option>
                            <option value="option2">IT业界</option>
                        </select>
                    </div>
                </div>
                <div class="am-u-sm-12 am-u-md-3">
                    <form>
                        <div class="am-input-group am-input-group-sm">
                            <input type="text" class="am-form-field" name="keyword" value="{{Request::input('keyword')}}">
                            <span class="am-input-group-btn">
                            <button class="am-btn am-btn-default" type="button">搜索</button>
                            <a href="{{route('shop.brand.index')}}" class="am-btn am-btn-default">重置</a>
                        </span>
                        </div>
                    </form>
                </div>
            </div>

            <div class="am-g">
                <div class="am-u-sm-12">
                    <form class="am-form">
                        <table class="am-table am-table-striped am-table-hover table-main">
                            <thead>
                            <tr>
                                <th class="table-check">
                                    <input type="checkbox" class="check_all"/>
                                </th>
                                <th>ID</th>
                                <th>缩略图</th>
                                <th>品牌名称</th>
                                <th>URL地址</th>
                                <th>品牌商品</th>
                                <th>品牌描述</th>
                                <th>是否显示</th>
                                <th>排序</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>

                            @foreach($brands as $brand)
                                <tr data-id="{{$brand->id}}">
                                    <td>
                                        <input type="checkbox" name="check_id[]" value="{{$brand->id}}" class="checked_id"/>
                                    </td>
                                    <td>{{$brand->id}}</td>
                                    <td>
                                        <img src="{{$brand->thumb}}" alt="" class="am-circle" width="50px"
                                             height="50px">
                                    </td>
                                    <td>{{$brand->name}}</td>
                                    <td>
                                        <a href="{{$brand->url}}" target="_blank">{{$brand->url}}</a>
                                    </td>
                                    <td>
                                        {!! show_brand_products($brand) !!}
                                    </td>
                                    <td>{{str_limit($brand->description,15,'...')}}</td>
                                    <td>
                                        {!! is_something($brand,'is_show') !!}
                                    </td>
                                    <td>
                                        <input type="hidden" name="id[]" value="{{$brand->id}}">
                                        <input type="text" class="sort_order" name="sort_order[]" value="{{$brand->sort_order}}" style="width:60px;">
                                    </td>
                                    <td>
                                        <div class="am-btn-toolbar">
                                            <div class="am-btn-group am-btn-group-xs">
                                                <a href="{{route('shop.brand.edit',$brand->id)}}"
                                                   class="am-btn am-btn-default am-btn-xs am-text-secondary"><span
                                                            class="am-icon-pencil-square-o"></span> 编辑
                                                </a>
                                                <a href="javascript:;" class="delete am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only">
                                                    <span class="am-icon-trash-o"></span> 删除
                                                </a>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                            @endforeach

                            </tbody>
                        </table>
                        <div class="am-cf">
                            共 {{$brands->total()}} 条记录
                            <div class="am-fr">
                                <ul class="am-pagination">
                                    {{ $brands->appends(Request::all())->links() }}
                                </ul>
                            </div>
                        </div>
                        <hr/>
                    </form>
                </div>

            </div>
        </div>

        <footer class="admin-content-footer">
            <hr>
            <p class="am-padding-left">© 2014 AllMobilize, Inc. Licensed under MIT license.</p>
        </footer>

    </div>
@endsection

@section('js')
    <script>
        $(function () {
            //改变属性
            $(".change_attr").click(function () {
                var id = $(this).parents('tr').data('id');
                var attr = $(this).attr('data-attr');
                var _this = $(this);
                $.ajax({
                    type: 'PATCH',
                    url: '{{route('shop.brand.change_attr')}}',
                    data: {id:id,attr:attr},
                    success: function () {
                        _this.toggleClass('am-icon-check am-icon-close');
                    }
                })
                return false;
            });

            //单个删除
            $('.delete').click(function () {
                var id = $(this).parents('tr').data('id');
                $.ajax({
                    type : 'DELETE',
                    url : '/admin/shop/brand/'+id,
                    data : {id:id},
                    success : function (data) {
                        $("tr[data-id='" + id + "']").fadeOut(400);
                    }
                });
                return false;
            });


            //上面全选，下面也选中
            $('.check_all').click(function () {
                var checked = $(this).prop('checked');
                $('.checked_id').prop('checked', checked);
            })
            //下面全选，上面也选中
            $('.checked_id').click(function () {
                var check_length = $('.checked_id:checked').length;
                var all_length = $('.checked_id').length;
                if(check_length == all_length){
                    $('.check_all').prop('checked',true);
                }else {
                    $('.check_all').prop('checked',false);
                }
            })

            //多选删除
            $('.delete_all').click(function () {
                var length = $('.checked_id:checked').length;
                if(length == 0){
                    alert('请你至少选择一条');return false;
                }

                $.ajax({
                    type : 'DELETE',
                    url : '{{route('shop.brand.delete_all')}}',
                    data : $('.checked_id:checked').serialize(),
                    success : function () {
                        $('.checked_id:checked').parents('tr').fadeOut(400);
                    }
                });
                return false;
            });

            //排序
            $('.sort_order').change(function () {
                var id = $(this).parents('tr').data('id');
                var sort_order = $(this).val();

                $.ajax({
                    type : 'PATCH',
                    url : '{{route('shop.brand.sort_order')}}',
                    data : {id:id,sort_order:sort_order},
                    success : function (data) {
                        window.location.reload();
                    }
                });
            })
        })
    </script>
@endsection
